package com.tensquare.user.service;

import com.tensquare.entity.IdWorker;
import com.tensquare.user.dao.AdminDao;
import com.tensquare.user.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * Created by WF on 2020/12/22 14:38
 */
@Service
public class AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder encoder;
    //1.添加管理员
    public void add(Admin admin) {
        //1.0)设置管理员id
        long id = idWorker.nextId();
        admin.setId(id + "");
        //1.1)将密码进行加密
        admin.setPassword(encoder.encode(admin.getPassword()));
        //1.2)保存用户
        adminDao.save(admin);
    }

    //2.管理员登录
    public Admin login(Admin admin) {
        //2.1)根据登录名查询用户
        Admin loginAdmin = adminDao.findByLoginname(admin.getLoginname());
        //2.2)判断是否有值
        if(loginAdmin != null && encoder.matches(admin.getPassword(),loginAdmin.getPassword())){
            return loginAdmin;
        }
        return null;
    }
    //删除管理员
    public void deleteById(String adminId) {
        adminDao.deleteById(adminId);
    }
}
